// i.d(t, { gm: () => Catalog, ne: () => DataCatalog, k1: () => s })

import Logger from "./Logger"
const n = new Logger("Subscription")
export class Catalog {
  watchedData: any
  registerSubscription: any
  unregisterSubscription: any
  constructor(e, t, i) {
    ;(this.watchedData = e), (this.registerSubscription = t), (this.unregisterSubscription = i)
  }
  createSubscription(e, t) {
    return new Subscription(
      () => this.registerSubscription(e),
      () => this.unregisterSubscription(e),
      t
    )
  }
  getData() {
    return this.watchedData()
  }
}
export class DataCatalog {
  watchedData: any
  registerSubscription: any
  unregisterSubscription: any
  constructor(e, t, i) {
    this.watchedData = e
    this.registerSubscription = t
    this.unregisterSubscription = i
  }
  createSubscription(e: Function, isSubbed?: boolean) {
    const i = () => e(this.watchedData())
    return new Subscription(
      () => this.registerSubscription(i),
      () => this.unregisterSubscription(i),
      isSubbed
    )
  }
  getData() {
    return this.watchedData()
  }
}
export class Subscription {
  startSubscription: Function
  endSubscription: Function
  id: string
  isSubbed: boolean
  constructor(start: Function, end: Function, isSubbed = !1, id = "") {
    this.startSubscription = start
    this.endSubscription = end
    this.id = id
    this.isSubbed = !1
    isSubbed && this.renew()
  }
  renew() {
    this.isSubbed ? n.debugWarn(`Duplicate subscription renew ${this.id}`) : (this.startSubscription(), (this.isSubbed = !0))
  }
  cancel() {
    this.isSubbed && (this.endSubscription(), (this.isSubbed = !1))
  }
  get active() {
    return this.isSubbed
  }
}
export function createSubscription(start: Function, end: Function, i = !0, n = "") {
  return new Subscription(start, end, i, n)
}
